﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Cart
{
    /// <summary>
    /// 获取所有购物车
    /// </summary>
    /// <returns></returns>
    public DataTable GetCart()
    {
        DataModelDataContext data = new DataModelDataContext();
        var result = data.Pr_GetAllCarts();
        DataTable table = new DataTable();
        table = result.CopyToDataTable();
        return table;
    }
    /// <summary>
    /// 获得某个用户的购物车
    /// </summary>
    /// <param name="userID"></param>
    /// <returns></returns>
    public DataTable GetCartByUser(int userID)
    {
        DataModelDataContext data = new DataModelDataContext();
        var result = data.Pr_GetCartByUser(userID);
        DataTable table = new DataTable();
        table = result.CopyToDataTable();
        return table;
    }
    /// <summary>
    /// 购物车小计
    /// </summary>
    /// <param name="userID"></param>
    /// <param name="kind"></param>
    /// <param name="money"></param>
    /// <param name="discount"></param>
    /// <returns></returns>
    public void GetCartByUser(int userID, out int kind, out double money, out double discount)
    {
        DataModelDataContext data = new DataModelDataContext();
        var result = data.Pr_GetCartByUser1(userID);
        DataTable table = new DataTable();
        table = result.CopyToDataTable();
        if (table != null)
        {
            kind = Convert.ToInt32(table.Rows[0]["kindnum"].ToString());
            money = Convert.ToDouble(table.Rows[0]["moneysum"].ToString());
            discount = Convert.ToDouble(table.Rows[0]["moneydis"].ToString());
        }
        else
        {
            kind = 0;
            money = 0;
            discount = 0;
        }
    }
    /// <summary>
    /// 向购物车中放东西
    /// </summary>
    /// <param name="bookID"></param>
    /// <param name="quantity"></param>
    /// <param name="userID"></param>
    /// <returns></returns>
    public int add(int instruID, int quantity, int userID)
    {
        DataModelDataContext data = new DataModelDataContext();
        DataTable table = new DataTable();
        int id = data.Pr_AddCart(instruID, quantity, userID);
        return id;
    }
    /// <summary>
    /// 更换购物车中某种商品的数量
    /// </summary>
    /// <param name="id"></param>
    /// <param name="quantity"></param>
    public void updatecart(int id, int quantity)
    {
        //声明定义类型和方法的DataModelDataContext类的对象，它是调用模型和方法的基础。
        DataModelDataContext data = new DataModelDataContext();
        ///定义保存从数据库获取的结果的DataTable
        DataTable table = new DataTable();
        //调用方法Pr_UpdateCart更改购物车某乐器数量
        data.Pr_UpdateCart(id, quantity);
    }
    /// <summary>
    /// 从购物车拿下某种商品
    /// </summary>
    /// <param name="id"></param>
    public void deletecart(int id)
    {
        //声明定义类型和方法的DataModelDataContext类的对象，它是调用模型和方法的基础。
        DataModelDataContext data = new DataModelDataContext();
        ///定义保存从数据库获取的结果的DataTable
        DataTable table = new DataTable();
        //调用方法Pr_DeleteCart获得所有的书籍信息
        data.Pr_DeleteCart(id);
    }
    /// <summary>
    /// 清空某个用户的购物车
    /// 当用户退出网站时,该用户的购物车被清空
    /// </summary>
    /// <param name="userID"></param>
    public void deletecartbyUser(int userID)
    {
        //声明定义类型和方法的DataModelDataContext类的对象，它是调用模型和方法的基础。
        DataModelDataContext data = new DataModelDataContext();
        ///定义保存从数据库获取的结果的DataTable
        DataTable table = new DataTable();
        //调用方法Pr_DeleteCartByClient获得所有的书籍信息
        data.Pr_DeleteCartByClient(userID);
    }
}
